package com.tinklabs.demo.utils;

import android.app.Activity;
import android.content.Context;
import android.graphics.Rect;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

/**
 * @author Jennifer
 * @date 2017/4/26. 21:45
 * @editor
 * @date
 * @describe
 */
public class ScreenUtils {

    public static int getStatusHeight(Activity activity) {
        Rect rect = new Rect();
        activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(rect);
        int height;
        if ((height = rect.top) == 0) {
            try {
                Class clazz;
                Object obj = (clazz = Class.forName("com.android.internal.R$dimen")).newInstance();
                int barHeight = Integer.parseInt(clazz.getField("status_bar_height").get(obj).toString());
                height = activity.getResources().getDimensionPixelSize(barHeight);
            } catch (ClassNotFoundException var4) {
                ;
            } catch (IllegalAccessException var5) {
                ;
            } catch (InstantiationException var6) {
                ;
            } catch (NumberFormatException var7) {
                ;
            } catch (IllegalArgumentException var8) {
                ;
            } catch (SecurityException var9) {
                ;
            } catch (NoSuchFieldException var10) {
                ;
            }
        }

        return height;
    }

    public static int getScreenHeight(Activity activity) {
        DisplayMetrics displaymetrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
        return displaymetrics.heightPixels;
    }

    public static boolean checkDeviceHasNavigationBar(Context context) {
        boolean isHad = false;
        int var2;
        if ((var2 = context.getResources().getIdentifier("config_showNavigationBar", "bool", "android")) > 0) {
            isHad = context.getResources().getBoolean(var2);
        }

        try {
            Class clazz;
            String var5 = (String) (clazz = Class.forName("android.os.SystemProperties")).getMethod("get", new Class[]{String.class}).invoke(clazz, new Object[]{"qemu.hw.mainkeys"});
            if ("1".equals(var5)) {
                isHad = false;
            } else if ("0".equals(var5)) {
                isHad = true;
            }
        } catch (Exception var3) {
            var3.printStackTrace();
        }

        return isHad;
    }

    public static int getNavigationBarHeight(Context context) {
        int var1 = 0;
        int var2;
        if ((var2 = context.getResources().getIdentifier("navigation_bar_height", "dimen", "android")) > 0 && checkDeviceHasNavigationBar(context)) {
            var1 = context.getResources().getDimensionPixelSize(var2);
        }

        return var1;
    }

    public static void setFullScreen(Context context) {
        //设置无标题
        ((Activity) context).requestWindowFeature(Window.FEATURE_NO_TITLE);
        //设置全屏
        ((Activity) context).getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    }


    public static void getDpi(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();

        float density = dm.density;        // 屏幕密度（像素比例：0.75/1.0/1.5/2.0）
        int densityDPI = dm.densityDpi;     // 屏幕密度（每寸像素：120/160/240/320）
        float xdpi = dm.xdpi;
        float ydpi = dm.ydpi;

        LogUtils.I("SAP", "屏幕密度 宽 = " + xdpi + "; 高 = " + ydpi);
        LogUtils.I("SAP", "屏幕密度比例 = " + density + "; 屏幕密度值 = " + densityDPI);
    }

    /**
     * 像素比例：0.75/1.0/1.5/2.0
     *
     * @param context
     * @return
     */
    public static float getPixelPercent(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();
        return dm.density;
    }

    /**
     * 每寸像素：120/160/240/320
     *
     * @param context
     * @return
     */
    public static int getDensityDPI(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();
        return dm.densityDpi;
    }

    /**
     * @param context
     * @return
     */
    public static float getDpiWidth(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();
        return dm.xdpi;
    }

    /**
     * @param context
     * @return
     */
    public static float getDpiHeight(Context context) {
        DisplayMetrics dm = new DisplayMetrics();
        dm = context.getResources().getDisplayMetrics();
        return dm.ydpi;
    }

    /**
     * 获取 屏幕 宽度
     *
     * @param activity
     * @return
     */
    public static int screenWidth(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);
        return metrics.widthPixels;
    }

    /**
     * 获取 屏幕 高度
     *
     * @param activity
     * @return
     */
    public static int screenHeight(Activity activity) {
        DisplayMetrics metrics = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(metrics);

        return metrics.heightPixels;
    }

    /**
     * 根据手机的分辨率从 dp 的单位 转成为 px(像素)
     */
    public static int dp2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    public static int dp2px(Context context, double dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     *
     * @param context
     * @param pxValue
     * @return
     */
    public static int px2dp(Context context, float pxValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (pxValue / scale + 0.5f);
    }

    /**
     * 将px值转换为sp值，保证文字大小不变
     *
     * @param context
     * @param pxValue （DisplayMetrics类中属性scaledDensity）
     * @return
     */
    public static int px2sp(Context context, float pxValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (pxValue / fontScale + 0.5f);
    }

    /**
     * 将sp值转换为px值，保证文字大小不变
     *
     * @param context
     * @param spValue
     * @return
     */
    public static int sp2px(Context context, float spValue) {
        final float fontScale = context.getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

}
